﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SwitchDetect
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Read train data.");
            // remove the second parameter to read all train data
            var trainData = InputData.ReadData(@"..\..\dataset\train", 1000000);

            ISolver solver = new Solver();
            Console.WriteLine("Train data.");
            solver.Train(trainData);

            Console.WriteLine("Calculate result on train data.");
            var result = solver.Solve(trainData);
            Console.WriteLine("AUC = {0}%", CheckResult(trainData, result));

            Console.WriteLine("Read test data.");
            var testData = InputData.ReadData(@"..\..\dataset\test");

            Console.WriteLine("Calculate result on test data.");
            result = solver.Solve(testData);
            SaveResult(result);

            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        private static void SaveResult(IEnumerable<int> result)
        {
            using (var sw = new StreamWriter("result.txt", false))
            {
                foreach (var id in result)
                {
                    sw.WriteLine(id);
                }
            }
        }

        private static double CheckResult(InputData data, IEnumerable<int> result)
        {
            var values = result.Select(sessionId => data.AllSessions[sessionId].SwitchType != SwitchType.None).ToArray();
            long wrong = 0;
            long pCount = 0;
            for (int i = 0; i < values.Length; i++)
            {
                if (!values[i]) continue;
                wrong += i;
                pCount++;
            }
            wrong -= pCount*(pCount - 1)/2;
            double all = pCount*(values.Length - pCount);
            return 100.0*(1.0 - wrong/all);
        }


    }
}
